package com.music.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.music.entity.CmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 2 * @Author: 刘佳乐 :面向百度编程
 * 3 * @Date: 2020/12/16 8:28
 * 4
 */
@Component
public class CookieCmUser {

   @Bean
    public RedisUtils getRedisUtils(){
        return new RedisUtils();
    }

    public CmUser getUserFromCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String number=null;
        for (Cookie cook:cookies
        ) {
            if (cook.getName().equals("token")){
                number=cook.getValue();
            }
        }
        Object o1 = getRedisUtils().get(number);
        String s = JSONObject.toJSONString(o1);
        CmUser cmUser = JSON.parseObject(s, CmUser.class);
        return cmUser;
    }
}
